package src;

import javax.swing.*;
import java.awt.*;

public class CustomerDialog extends JDialog {
    private JTextField idField;
    private JTextField nameField;
    private JTextField idCardField;
    private JTextField phoneField;
    private JTextField addressField;
    private boolean confirmed = false;

    public CustomerDialog(Frame owner, String title) {
        super(owner, title, true);
        setSize(400, 300);
        setLocationRelativeTo(owner);

        JPanel panel = new JPanel(new GridLayout(6, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 添加输入字段
        panel.add(new JLabel("客户编号:"));
        idField = new JTextField();
        panel.add(idField);

        panel.add(new JLabel("客户姓名:"));
        nameField = new JTextField();
        panel.add(nameField);

        panel.add(new JLabel("身份证号:"));
        idCardField = new JTextField();
        panel.add(idCardField);

        panel.add(new JLabel("联系电话:"));
        phoneField = new JTextField();
        panel.add(phoneField);

        panel.add(new JLabel("联系地址:"));
        addressField = new JTextField();
        panel.add(addressField);

        // 添加按钮
        JButton confirmButton = new JButton("确认");
        JButton cancelButton = new JButton("取消");

        JPanel buttonPanel = new JPanel();
        buttonPanel.add(confirmButton);
        buttonPanel.add(cancelButton);
        panel.add(buttonPanel);

        // 添加事件监听器
        confirmButton.addActionListener(e -> {
            if (validateInput()) {
                confirmed = true;
                dispose();
            }
        });

        cancelButton.addActionListener(e -> dispose());

        add(panel);
    }

    private boolean validateInput() {
        if (idField.getText().trim().isEmpty() ||
            nameField.getText().trim().isEmpty() ||
            idCardField.getText().trim().isEmpty() ||
            phoneField.getText().trim().isEmpty() ||
            addressField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有字段！", "错误", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public String getCustomerId() {
        return idField.getText().trim();
    }

    public String getCustomerName() {
        return nameField.getText().trim();
    }

    public String getIdCard() {
        return idCardField.getText().trim();
    }

    public String getPhone() {
        return phoneField.getText().trim();
    }

    public String getAddress() {
        return addressField.getText().trim();
    }
} 